package com.ruoyi.web.controller.veterinary;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.veterinary.domain.VetPurchase;
import com.ruoyi.veterinary.service.IVetPurchaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 进货管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/veterinary/purchase")
public class VetPurchaseController extends BaseController
{
    @Autowired
    private IVetPurchaseService vetPurchaseService;

    /**
     * 查询进货管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(VetPurchase vetPurchase)
    {
        startPage();
        List<VetPurchase> list = vetPurchaseService.selectVetPurchaseList(vetPurchase);
        return getDataTable(list);
    }

    /**
     * 导出进货管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:export')")
    @Log(title = "进货管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VetPurchase vetPurchase)
    {
        List<VetPurchase> list = vetPurchaseService.selectVetPurchaseList(vetPurchase);
        ExcelUtil<VetPurchase> util = new ExcelUtil<VetPurchase>(VetPurchase.class);
        util.exportExcel(response, list, "进货管理数据");
    }

    /**
     * 获取进货管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:query')")
    @GetMapping(value = "/{purchaseId}")
    public AjaxResult getInfo(@PathVariable("purchaseId") Long purchaseId)
    {
        return success(vetPurchaseService.selectVetPurchaseByPurchaseId(purchaseId));
    }

    /**
     * 新增进货管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:add')")
    @Log(title = "进货管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VetPurchase vetPurchase)
    {
        return toAjax(vetPurchaseService.insertVetPurchase(vetPurchase));
    }

    /**
     * 修改进货管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:edit')")
    @Log(title = "进货管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VetPurchase vetPurchase)
    {
        return toAjax(vetPurchaseService.updateVetPurchase(vetPurchase));
    }

    /**
     * 删除进货管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:remove')")
    @Log(title = "进货管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseIds}")
    public AjaxResult remove(@PathVariable Long[] purchaseIds)
    {
        return toAjax(vetPurchaseService.deleteVetPurchaseByPurchaseIds(purchaseIds));
    }

    /**
     * 生成采购单号
     */
    @GetMapping("/generateOrderNo")
    public AjaxResult generateOrderNo()
    {
        String orderNo = vetPurchaseService.generatePurchaseOrderNo();
        return success(orderNo);
    }

    /**
     * 查询待验收采购单列表
     */
    @GetMapping("/pending")
    public AjaxResult getPendingList()
    {
        List<VetPurchase> list = vetPurchaseService.selectPendingAcceptanceList();
        return success(list);
    }

    /**
     * 验收入库操作
     */
    @PreAuthorize("@ss.hasPermi('veterinary:purchase:edit')")
    @Log(title = "验收入库", businessType = BusinessType.UPDATE)
    @PostMapping("/acceptance")
    public AjaxResult processAcceptance(@RequestBody VetPurchase vetPurchase)
    {
        try {
            return toAjax(vetPurchaseService.processAcceptance(vetPurchase));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}